package view;

import javafx.geometry.Pos;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.ImageView;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import logic.FileSystem;
import logic.MyFile;
import logic.RespBean;
import view.controller.DiskController;
import view.controller.NewFileWindowController;
import view.controller.NewFolderWindowController;

public class newFolderWindow {
    static FileSystem fileSystem = FileSystem.getInstance();
    public static void display(Parent root, NewFolderWindowController controller, DiskController diskController){
        Stage stage = new Stage();
        Scene scene = new Scene(root);

        controller.submit.setOnMouseClicked(mouseEvent -> {
            submit(controller,diskController);
            stage.close();
        });

        stage.setScene(scene);
        stage.showAndWait();

    }
    public static void submit(NewFolderWindowController controller, DiskController diskController){
        //System.out.println("submit");
        String name = controller.foldername.getText();
        String type = null;
        RespBean respBean = fileSystem.nameValidityTest(name, type);
        boolean flag = respBean.isRes();
        //System.out.println(flag);
        if(flag){
            //todo 添加到list
            MyFile myFile = (MyFile) fileSystem.createDirectory(name).getObj();
            Button bu = new Button();
            ImageView img = new ImageView(newFileWindow.class.getClassLoader().getResource("Folder.jpg").toString());
            img.setFitHeight(70);
            img.setFitWidth(70);
            bu.setPrefSize(70,80);
            diskController.addRClick(bu);
            VBox vBox = new VBox();
            vBox.setAlignment(Pos.CENTER);
            vBox.getChildren().add(img);
            vBox.getChildren().add(new Label(myFile.getName()));
            bu.setGraphic(vBox);
            diskController.map.put(bu,myFile);
            diskController.flush();
        }
        else{
            Alert alert = new Alert(Alert.AlertType.ERROR);
            alert.setTitle("命名错误！");
            alert.setContentText("命名不规范："+respBean.getMessage());
            alert.show();
        }
    }

    public static void display2(Parent root, NewFolderWindowController controller, DiskController diskController, Button button){
        Stage stage = new Stage();
        Scene scene = new Scene(root);
        MyFile file = diskController.map.get(button);
        controller.getFoldername().setText(file.getName());
        controller.getSubmit().setOnMouseClicked(mouseEvent -> {
            modify(controller,diskController,button);
            stage.close();
        });

        stage.setScene(scene);
        stage.showAndWait();
    }

    public static void modify(NewFolderWindowController controller, DiskController diskController,Button button){
        //System.out.println("modify");
        String name = controller.getFoldername().getText();
        RespBean respBean = fileSystem.nameValidityTest(name, null);
        boolean flag = respBean.isRes();
        //System.out.println(flag);
        if(flag){
            //todo 添加到list
            MyFile myFile = (MyFile) fileSystem.modifyDirectory(diskController.map.get(button),name).getObj();
            diskController.map.remove(button);
            Button bu = new Button();
            ImageView img = new ImageView(newFileWindow.class.getClassLoader().getResource("Folder.jpg").toString());
            img.setFitHeight(70);
            img.setFitWidth(70);
            bu.setPrefSize(70,80);
            diskController.addRClick(bu);
            VBox vBox = new VBox();
            vBox.setAlignment(Pos.CENTER);
            vBox.getChildren().add(img);
            vBox.getChildren().add(new Label(myFile.getName()));
            bu.setGraphic(vBox);
            diskController.map.put(bu,myFile);
            diskController.flush();
        }
        else{
            Alert alert = new Alert(Alert.AlertType.ERROR);
            alert.setTitle("命名错误！");
            alert.setContentText("命名不规范："+respBean.getMessage());
            alert.show();
        }
    }
}
